﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ApiBetaSeries.Factories;

namespace ApiBetaSeries.Services
{
    public class ShowServices : ApiBetaSeries.Services.IShowServices
    {

        #region singleton
        private static ShowServices m_showServices = null;
        private ShowServices() { }
        public static ShowServices GetInstance()
        {
            if (m_showServices == null)
            {
                m_showServices = new ShowServices();
            }
            return m_showServices;
        }
        #endregion
        /// <summary>
        /// Get a Show from its "url"
        /// </summary>
        /// <param name="url">Url of the show</param>
        /// <returns>The Show</returns>
        [ApiBetaSeries.Version.ApiMethod(true, "shows/display")]
        public IShow GetShow(string url)
        {
            XmlDocument doc = Utils.UtilsXML.GetUrl("/shows/display/" + url + ".xml");
            IShow s;
            XmlElement xmlshow = doc["root"]["show"];

            if (xmlshow == null)
            {
                throw new BetaSeriesException(1003, "Invalid action.");
            }

            s = ShowFactory.CreateShow(xmlshow);
            return s;
        }

        /// <summary>
        /// Get all the shows and theirs url
        /// </summary>
        /// <returns>couple <name, url><Title, url></returns>
        [ApiBetaSeries.Version.ApiMethod(true, "shows/display")]
        public Dictionary<string, string> GetAllShows()
        {
            Dictionary<string, string> shows = new Dictionary<string, string>();
            XmlDocument doc = Utils.UtilsXML.GetUrl("/shows/display/all.xml");
            XmlElement xmlshows = doc["root"]["shows"];

            foreach (XmlElement show in xmlshows.GetElementsByTagName("show"))
            {
                string title = show["title"].FirstChild.Value;
                string url = show["url"].FirstChild.Value;
                shows.Add(title, url);
            }

            return shows;
        }

        /// <summary>
        /// Search a show by title name
        /// </summary>
        /// <param name="search">search expression</param>
        /// <returns>couple <name, url><Title, url></returns>
        [ApiBetaSeries.Version.ApiMethod(true, "shows/search")]
        public Dictionary<string, string> SearchShow(string search)
        {
            Dictionary<string, string> shows = new Dictionary<string, string>();
            XmlDocument doc = Utils.UtilsXML.GetUrl("/shows/search.xml", "&title=" + search);
            XmlElement xmlshows = doc["root"]["shows"];

            foreach (XmlElement show in xmlshows.GetElementsByTagName("show"))
            {
                string title = show["title"].FirstChild.Value;
                string url = show["url"].FirstChild.Value;
                shows.Add(title, url);
            }

            return shows;
        }

        /// <summary>
        /// Recommend this show to a friend
        /// </summary>
        /// <param name="urlShow">url of the show</param>
        /// <param name="user">identified member</param>
        /// <param name="friend">friend's username</param>
        [ApiBetaSeries.Version.ApiMethod(false, "shows/recommend")]
        public void RecommendShow(string urlShow, IUser user, string friend)
        {
            RecommendShow(urlShow, user.Token, friend);
        }
        /// <summary>
        /// Recommend this show to a friend
        /// </summary>
        /// <param name="urlShow">url of the show</param>
        /// <param name="token">token of an identified member</param>
        /// <param name="friend">friend's username</param>
        [ApiBetaSeries.Version.ApiMethod(false, "shows/recommend")]
        public void RecommendShow(string urlShow, string token, string friend)
        {
            IShow show = ShowFactory.CreateShow();
            show.Url = urlShow;
            show.Recommend(token, friend);
        }
    }
}
